/**
 * 新增修改完成调用 modalFormOk方法 编辑弹框组件ref定义为modalForm
 * 高级查询按钮调用 superQuery方法  高级查询组件ref定义为superQueryModal
 * data中url定义 list为查询列表  delete为删除单条记录  deleteBatch为批量删除
 */
import {
	filterObj
} from '@/utils/util';
import {
	getAction,
	postAction
} from '@/api/manage'
import Vue from 'vue'
import {
	ACCESS_TOKEN
} from "@/store/mutation-types"

export const JeecgListMixin = {
	data() {
		return {
			//token header
			tokenHeader: {
				'X-Access-Token': Vue.ls.get(ACCESS_TOKEN)
			},
			/* 查询条件-请不要在queryParam中声明非字符串值的属性 */
			queryParam: {},
			/* 数据源 */
			dataSource: [],
			/* 分页参数 */
			ipagination: {
				current: 1,
				pageSize: 10,
				pageSizeOptions: ['10', '20', '30'],
				showTotal: (total, range) => {
					return range[0] + "-" + range[1] + " 共" + total + "条"
				},
				showQuickJumper: true,
				showSizeChanger: true,
				total: 0
			},
			/* 排序参数 */
			// isorter:{
			//   column: 'createTime',
			//   order: 'desc',
			// },
			/* 筛选参数 */
			filters: {},
			/* table加载状态 */
			loading: false,
			/* table选中keys*/
			selectedRowKeys: [],
			/* table选中records*/
			selectionRows: [],
			/* 查询折叠 */
			toggleSearchStatus: false,
		}
	},
	created() {
		if (!this.disableMixinCreated) {
			console.log(' -- mixin created -- ')


			if (this.url.list == '/dev/camera/all-page' || this.url.list == '/dev/page') {
				console.log('==============this.url.list==================')
				return;
			}

			this.loadData();
			//初始化字典配置 在自己页面定义
			this.initDictConfig();
		}
	},
	methods: {
		loadData(arg) {
			if (!this.url.list) {
				this.$message.error("请设置url.list属性!")
				return
			}
			//加载数据 若传入参数1则加载第一页的内容
			if (arg === 1) {
				this.ipagination.current = 1;
			}
			var params = this.getQueryParams(); //查询条件
			this.loading = true;

			if (this.domainType == '1') {
				console.log('=====domainType为1显示=====')
				this.showed = true
			} else {
				console.log('=====domainType为2隐藏=====')
				this.showed = false
			}

			postAction(this.url.list, params).then((res) => {
				if (res.code == "00000") {
					if (!res.data) {
						this.loading = false;
						return
					}

					this.dataSource = res.data.records;
					this.ipagination.total = res.data.total;
					this.selectedRowKeys = [];

					console.log(res.data.records)
					console.log(this.dataSource)
					console.log(this.ipagination.total)
					console.log('----------------发请求获取数据-----------------')

					
				} else {
					this.$message.warning(res.msg);
					// 没有查询到数据，数据初始化
					if (res.code == "A0404") {
						this.dataSource = [];
						this.ipagination.total = 0;
						this.selectedRowKeys = [];
					}
				}
				this.loading = false;
			})
		},
		initDictConfig() {
			console.log("--这是一个假的方法!")
		},
		getQueryParams() {
			//获取查询条件
			let sqp = {}
			var param = Object.assign(sqp, this.queryParam, this.filters);
			console.log('-------查找的数据------')
			// console.log(this.regionIdOne)
			// param.regionId = this.regionIdOne;
			param.current = this.ipagination.current;
			param.size = this.ipagination.pageSize;
			return filterObj(param);

		},
		onSelectChange(selectedRowKeys, selectionRows) {
			this.selectedRowKeys = selectedRowKeys;
			this.selectionRows = selectionRows;
		},
		onClearSelected() {
			this.selectedRowKeys = [];
			this.selectionRows = [];
		},
		searchQuery() {
			this.loadData(1);
		},
		superQuery() {
			this.$refs.superQueryModal.show();
		},
		searchReset() {
			this.queryParam = {}
			this.queryParam.mobile = ""
			this.queryParam.realName = ""
			this.loadData(1);
		},
		batchDel: function (type) {
			if (!this.url.deleteBatch) {
				this.$message.error("请设置url.deleteBatch属性!")
				return
			}
			if (this.selectedRowKeys.length <= 0) {
				this.$message.warning('请选择一条记录！');
				return;
			} else {
				// var ids = "";
				console.log(this.selectedRowKeys)
				// for (var a = 0; a < this.selectedRowKeys.length; a++) {
				//   this.selectedRowKeys[a] = parseInt(this.selectedRowKeys[a])
				// }
				var that = this;
				this.$confirm({
					title: "确认删除",
					content: "是否删除选中数据?",
					onOk: function () {
						that.loading = true;
						let deleteyParam = {};
						if (type == 2) deleteyParam["ids"] = that.selectedRowKeys;
						else deleteyParam["idList"] = that.selectedRowKeys;
						postAction(that.url.deleteBatch, deleteyParam).then((res) => {
							if (res.code == "00000") {
								let msg = "删除成功"
								if (res.msg.toLowerCase() !== "ok") {
									msg = res.msg;
								}
								that.$message.success(msg);
								that.loadData();
								that.onClearSelected();
							} else {
								that.$message.warning(res.msg);
							}
						}).finally(() => {
							that.loading = false;
						});
					}
				});
			}
		},
		batchOpt: function (urlStr, used) {
			if (this.selectedRowKeys.length <= 0) {
				this.$message.warning('请选择一条记录！');
				return;
			} else {
				var that = this;
				this.$confirm({
					title: "确认操作",
					content: "确认要进行此操作?",
					onOk: function () {
						that.loading = true;
						let optParam = {};
						optParam["idlist"] = that.selectedRowKeys;
						optParam["used"] = used;
						console.log(">>>>>>urlStr:" + urlStr)
						console.log(">>>>>>optParam:" + JSON.stringify(optParam))
						postAction(urlStr, optParam).then((res) => {
							if (res.code == "00000") {
								let msg = "操作成功"
								if (res.msg.toLowerCase() !== "ok") {
									msg = res.msg;
								}
								that.$message.success(msg);
								that.loadData();
								that.onClearSelected();
							} else {
								that.$message.warning(res.msg);
							}
						}).finally(() => {
							that.loading = false;
						});
					}
				});
			}
		},
		handleDelete: function (id) {
			if (!this.url.delete) {
				this.$message.error("请设置url.delete属性!")
				return
			}
			var that = this;
			getAction(that.url.delete + "/" + id).then((res) => {
				if (res.code == "00000") {
					that.$message.success("删除成功");
					that.loadData();
				} else {
					that.$message.warning("删除失败");
				}
			});
		},
		handleEdit: function (record) {
			console.log(record,'=======modalFormderecord======')
			this.$refs.modalForm.edit(record);
			this.$refs.modalForm.title = "编辑";
			this.$refs.modalForm.disableSubmit = false;
		},
		handleAdd: function (addWay) {
			if (addWay != undefined) {
				this.$refs.modalForm.addWay = addWay;
			}
			this.$refs.modalForm.add();
			this.$refs.modalForm.title = "新增";
			this.$refs.modalForm.disableSubmit = false;
		},
		handleAssing: function (record) {
			this.$refs.modalForm.edit(record);
			this.$refs.modalForm.title = "分派";
			this.$refs.modalForm.disableSubmit = false;
		},
		handleReassing: function (record) {
			this.$refs.modalForm.edit(record);
			this.$refs.modalForm.title = "改派";
			this.$refs.modalForm.disableSubmit = false;
		},
		handleTableChange(pagination) {
			//分页、排序、筛选变化时触发
			//TODO 筛选
			// if (Object.keys(sorter).length > 0) {
			//   this.isorter.column = sorter.field;
			//   this.isorter.order = "ascend" == sorter.order ? "asc" : "desc"
			// }
			this.ipagination = pagination;
			this.loadData();
		},
		// 给popup查询使用(查询区域不支持回填多个字段，限制只返回一个字段)
		getPopupField(fields) {
			return fields.split(',')[0]
		},
		modalFormOk() {
			// 新增/修改 成功时，重载列表
			this.loadData();
		},
		handleDetail: function (record) {
			this.$refs.modalForm.edit(record);
			this.$refs.modalForm.title = "详情";
			this.$refs.modalForm.disableSubmit = true;
		},
		handleMaintain: function (record) {
			this.$refs.modalForm.edit(record);
			this.$refs.modalForm.title = "工单维护";
			this.$refs.modalForm.disableSubmit = false;
		},
		/* 导入 */
		//只在长者档案中使用
		handleImportExcel(info) {
			if (info.file.status !== 'uploading') {
				console.log(info.file, info.fileList);
			}
			if (info.file.status === 'done') {
				console.log(info.file)
				if (info.file.response.code === '00000') {
					this.$message.success(`${info.file.name} 文件上传成功`)
					this.loadData()
				} else {
					this.$message.error(`${info.file.name} 文件上传失败`);
					this.$refs.alertModalForm.alertData = info.file.response.data
					this.$refs.alertModalForm.show()
				}
			} else if (info.file.status === 'error') {
				this.$message.error(`文件上传失败: ${info.file.msg} `);
			}
		},
	}

}